package com.canbot.u05.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.activity.customvoice.ChooseAudioActivity;
import com.canbot.u05.activity.shoppingGuide.ShoppingChooseAudioActivity;
import com.canbot.u05.bean.CustomDialogue;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.bean.StandbyVoice;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.MyDbUtils;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.CenterDialogerror;
import com.canbot.u05.view.UsherModeRecordDialog;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 音频选择界面
 *
 * @author ruandan
 */
public class RecordActivity extends BaseActivityNoTitle implements
        OnClickListener {

        public static final String TAG = "RecordActivity";
        private RelativeLayout rlFile;

        private RelativeLayout rlCreate;

        private RelativeLayout rlrecord;

        private CheckBox cbCreate;

        private CheckBox cbFile;

        private CheckBox cbrecord;

        private Button btnOk;

        private Button btnBack;

        private   String musicPath;

        private String question;

        private CenterDialogerror successDialog;

        private CenterDialogerror failDialog;

        private String whichSensor;// 来自的感应器界面

        private TextView tvHead;

        private Dialog dialogLoading;

        private int fromActivity;

        private  int voiceType;

        private String voiceStr;

        private UsherModeRecordDialog usherModeRecordDialog;

        private String fileName;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_record);
                fromActivity = getIntent().getIntExtra("fromActivity",-1);
                initView();
                setTitle();

        }

        private void setTitle() {
                switch (fromActivity) {
                        case ConstUtils.ACTIVITY_SENSOR:
                                tvHead.setText("自定义感应反馈");
                                break;
                        case ConstUtils.ACTIVITY_STANDBY_VOICE:
                                tvHead.setText("自定义待机语音");
                                break;
                        case ConstUtils.ACTIVITY_VCC:
                                tvHead.setText("自定义声控对话");
                                break;
                        case ConstUtils.ACTIVITY_START_VOICE:
                                tvHead.setText("自定义开机语音");
                                break;
                        case ConstUtils.ACTIVITY_SENSOR_PEOPLE:
                                tvHead.setText("自定义人体感应语音");
                                break;
                }

        }

        private void initView() {
                rlFile = (RelativeLayout) findViewById(R.id.rlFile);
                rlCreate = (RelativeLayout) findViewById(R.id.rlCreate);
                rlrecord = (RelativeLayout) findViewById(R.id.rlrecord);
                cbCreate = (CheckBox) findViewById(R.id.cbCreate);
                cbFile = (CheckBox) findViewById(R.id.cbFile);
                cbrecord = (CheckBox) findViewById(R.id.cbrecord);
                btnBack = (Button) findViewById(R.id.btn_head_back);
                btnOk = (Button) findViewById(R.id.btnOk);
                tvHead = (TextView) findViewById(R.id.tv_head);
                rlFile.setOnClickListener(this);
                btnBack.setOnClickListener(this);
                rlCreate.setOnClickListener(this);
                btnOk.setOnClickListener(this);
                rlrecord.setOnClickListener(this);
                if(fromActivity == ConstUtils.ACTIVITY_SENSOR_PEOPLE){
                        rlrecord.setVisibility(View.VISIBLE);
                }
                if (fromActivity == ConstUtils.ACTIVITY_VCC) {// 自定义对话
                        question = getIntent().getStringExtra("question");
                }else if (fromActivity == ConstUtils.ACTIVITY_SENSOR
                        || fromActivity == ConstUtils.ACTIVITY_START_VOICE
                        || fromActivity == ConstUtils.ACTIVITY_SENSOR_PEOPLE ) {// 自定义感应器或自定义开机或人体感应器
                        whichSensor = getIntent().getStringExtra("from");
                }
                successDialog = new CenterDialogerror(this, R.style.centerdialog,
                        getString(R.string.custom_success));
                failDialog = new CenterDialogerror(this, R.style.centerdialog, getString(R.string.custom_failed));

        }

        @Override
        protected void onResume() {
                super.onResume();
                Logger.e("storeStandbyVoiceLocal", voiceType + "onResume");
                // 设置所选择的音频文件
                if (voiceType == ConstUtils.VOICE_TYPE_FILE) {// 音频文件
                        cbFile.setChecked(true);
                        cbCreate.setChecked(false);
                        btnOk.setBackgroundResource(R.drawable.standby_complete);
                }
                else if (voiceType == ConstUtils.VOICE_TYPE_TTS) {// TTS文本
                        cbFile.setChecked(false);
                        cbCreate.setChecked(true);
                        btnOk.setBackgroundResource(R.drawable.standby_complete);
                }
                else if (voiceType == 0) {// 没有选择
                        cbFile.setChecked(false);
                        cbCreate.setChecked(false);
                        btnOk.setBackgroundResource(R.drawable.standby_complete01);
                }
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.rlFile:
                                cbFile.setChecked(true);
                                if (cbFile.isChecked() || cbCreate.isChecked()) {
                                        btnOk.setBackgroundResource(R.drawable.standby_complete);
                                }
                                else {
                                        btnOk.setBackgroundResource(R.drawable.standby_complete01);
                                }
//                                Intent intent1 = new Intent(this, AudioChooseActivity.class);
//                                intent1.putExtra("type", ConstUtils.CODE_SELECT_AUDIO);
//                                startActivityForResult(intent1, ConstUtils.CODE_SELECT_AUDIO);
                                //选择音频源修改为 从头部uusound中选取
                                Intent intent1 = new Intent(this, ChooseAudioActivity.class);
                                intent1.putExtra("path", "/uusound");
                                startActivityForResult(intent1, ConstUtils.CODE_SELECT_AUDIO);
                                voiceType = 1;// 标注类型
                                break;
                        case R.id.rlCreate:
                                cbCreate.setChecked(true);
                                if (cbFile.isChecked() || cbCreate.isChecked()) {
                                        btnOk.setBackgroundResource(R.drawable.standby_complete);
                                }
                                else {
                                        btnOk.setBackgroundResource(R.drawable.standby_complete01);
                                }

                                Intent intent2 = new Intent(this, Txt2VoiceActivity.class);
                                intent2.putExtra("type", ConstUtils.CODE_SELECT_AUDIO);
                                startActivityForResult(intent2, ConstUtils.CODE_SELECT_TTS);

                                voiceType = 2;
                                break;
                        case R.id.rlrecord:
                                cbrecord.setChecked(true);
                                btnOk.setBackgroundResource(R.drawable.standby_complete);
                                if (usherModeRecordDialog == null) {
                                        //点击语音录入图标
                                        usherModeRecordDialog = new UsherModeRecordDialog(this, UsherModeRecordDialog.Record);
                                }
                                usherModeRecordDialog.setFinishClickListener(this);
                                usherModeRecordDialog.setCancleClickListener(this);
                                setDialogWindowAttr(usherModeRecordDialog, this);
                                usherModeRecordDialog.show();
                                voiceType = 3;
                                break;
                        case R.id.btn_head_back:// 点击返回键，清除所有数据
                                finish();
                                break;
                        case R.id.btnOk:// 点击完成时，不清理数据并返回
                                handleVoice();// 储存语音
                                break;
                        case R.id.record_dialog_finish:
                                fileName = usherModeRecordDialog.ok();
                                Logger.e("test", "fileName" + fileName);
                                usherModeRecordDialog.setTime(RecordActivity.this.getString(R.string.uploading_please_wait));
                                updataFile();
                                voiceType=3;
                                break;
                        case R.id.record_dialog_cancle:
                                cbrecord.setChecked(false);
                                btnOk.setBackgroundResource(R.drawable.standby_complete01);
                                usherModeRecordDialog.no();
                                break;
                }

        }

        private void updataFile() {

                HashMap<String, Object> hasMap = new HashMap<>();

                File file = new File(ConstUtils.PATH_SD +"/record/" ,fileName);
                if (!file.exists()) {
                        file.mkdirs();
                }

                hasMap.put("path", fileName);
                hasMap.put("audiofile", file);
                hasMap.put("sensor","sensor");

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "uploadRecord", hasMap, new OkHttpUtils.OkHttpCallBack() {

                        @Override
                        public void onSuccess(String result) {

                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        usherModeRecordDialog.cancel();
                                                        runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                        ToastTools.newInstance(RecordActivity.this).showText(getString(R.string.upload_complete));
                                                                        finish();
                                                                }
                                                        });
                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                usherModeRecordDialog.cancel();
                                                ToastTools.newInstance(RecordActivity.this).showText(getString(R.string.network_request_failed));
                                        }
                                });
                        }
                });

        }

        /**
         * 处理自定义语音
         */
        private void handleVoice() {

                if (cbFile.isChecked() || cbCreate.isChecked() || cbrecord.isChecked()) {
                        int from = fromActivity;
                        switch (from) {
                                case ConstUtils.ACTIVITY_VCC:// 对话
                                        storeVcc();
                                        break;
                                case ConstUtils.ACTIVITY_START_VOICE:// 开机语音
                                        storeStartVoice();
                                        break;
                                case ConstUtils.ACTIVITY_SENSOR:// 感应器
                                        storeSensorVoice();
                                        break;
                                case ConstUtils.ACTIVITY_SENSOR_PEOPLE:// 人体感应器
                                        if (voiceType == 3) {
                                                updataFile();
                                        }
                                        else {
                                                storeSensorVoice();
                                        }
                                        break;
                                case ConstUtils.ACTIVITY_STANDBY_VOICE:
                                        storeStandbyVoice();// 储存音频文件
                                        break;
                        }
                        voiceType = 0;
                }
        }

        /**
         * 保存感应器语音
         */
        private void storeSensorVoice() {
                HashMap<String, Object> hasMap = new HashMap<>();

                if (ConstUtils.VOICE_TYPE_FILE == voiceType) { // 文件
                        hasMap.put("voicetype", ConstUtils.VOICE_TYPE_TTS);
                        hasMap.put("fileName", musicPath);
                }
                else if (ConstUtils.VOICE_TYPE_TTS == voiceType) {// TTS
                        hasMap.put("voicetype", ConstUtils.VOICE_TYPE_FILE);
                        hasMap.put("tts", voiceStr);
                }

                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                dialogLoading.show();

                hasMap.put("upload", "提交");
                hasMap.put("postype", whichSensor);

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "voicecustom", hasMap, new OkHttpUtils.OkHttpCallBack() {

                        @Override
                        public void onSuccess(String result) {

                                JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.dismiss();
                                                        storeSensorVoiceLocal();
//                                successDialog.show();
                                                }
                                        });
                                }
                                else {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.dismiss();
                                                        failDialog.show();
                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                dialogLoading.dismiss();
                                                ToastTools.newInstance(RecordActivity.this).showText(getString(R.string.network_request_failed));
                                        }
                                });
                        }
                });
                // 默认发送文本信息为成功，无需等待返回
        }


        /**
         * 保存感应器语音到本地
         */
        private void storeSensorVoiceLocal() {
                SharedPreferences sp = getSharedPreferences(whichSensor,
                        Context.MODE_PRIVATE);
                Editor editor = sp.edit();
                editor.putString("which", "custom");
                editor.apply();
                showDialog();
        }

        /**
         * 保存开机语
         */
        private void storeStartVoice() {
                HashMap<String, Object> hasMap = new HashMap<>();

                if (ConstUtils.VOICE_TYPE_FILE == voiceType) { // 文件
                        hasMap.put("voicetype", ConstUtils.VOICE_TYPE_TTS);
                        hasMap.put("fileName", musicPath);
                }
                else if (ConstUtils.VOICE_TYPE_TTS == voiceType) {// TTS
                        hasMap.put("voicetype", ConstUtils.VOICE_TYPE_FILE);
                        hasMap.put("tts", voiceStr);
                }

                        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                        dialogLoading.show();

                        hasMap.put("postype", "start_voice");
                        hasMap.put("upload", "提交");

                        OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "voicecustom", hasMap, new OkHttpUtils.OkHttpCallBack() {

                                @Override
                                public void onSuccess(String result) {
                                        JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
//                    Logger.e("save","onSuccess:"+"http://"+IpMsgConst.SOCKET_HOST2+ ":7755/"+"strMsgBean.getRc()"+strMsgBean.getRc());
                                        if (strMsgBean.getRc() == 0) {
                                                runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                                dialogLoading.dismiss();
                                                                storeStartVoiceLocal();
                                                        }
                                                });
                                        }
                                        else {
                                                runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                                dialogLoading.dismiss();
                                                                failDialog.show();
                                                        }
                                                });
                                        }
                                }

                                @Override
                                public void onFailure() {
                                        Logger.e("save", "onFailure" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/");
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.dismiss();
                                                        ToastTools.newInstance(RecordActivity.this).showText(getString(R.string.network_request_failed));
                                                }
                                        });
                                }
                        });
                        // 默认发送文本成功，无需等待返回结果
                }

        /**
         * 保存开机语到本地
         */
        private void storeStartVoiceLocal() {
                SharedPreferences sp = getSharedPreferences(whichSensor,
                        Context.MODE_PRIVATE);
                Editor editor = sp.edit();
                editor.putString("which", "custom");
                editor.apply();
                showDialog();
        }

        /**
         * 保存对话
         */
        private void storeVcc() {
                String answer = "";
                HashMap<String, Object> hasMap = new HashMap<>();

                if (ConstUtils.VOICE_TYPE_FILE == voiceType) { // 文件
                        answer = musicPath;
//                        File file = new File(musicPath);
//                        if (!file.exists()) {
//                                failDialog.show();// 失败提示
//                        }
//                        else {
                                hasMap.put("fileName", answer);
//                                hasMap.put("audiofile", new File(musicPath));
//                        }

                }
                else if (ConstUtils.VOICE_TYPE_TTS == voiceType) {// TTS
                        answer = voiceStr;
                        hasMap.put("tts", voiceStr);
                }

                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                dialogLoading.show();

                hasMap.put("question", question + "");
                hasMap.put("upload", "提交");
                hasMap.put("voicetype", voiceType);
                Logger.e("save", "voicetype" + voiceType);
                Logger.e("save", "question" + question);


                final String finalAnswer = answer;
                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "dialogcustom", hasMap, new OkHttpUtils.OkHttpCallBack() {

                        @Override
                        public void onSuccess(String result) {

                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("save", "onSuccess2222 file :" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/dialogcustom " + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() == 0) {

                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        ToastTools.newInstance(RecordActivity.this).showText(getString(R.string.network_request_succeeded));
                                                        MsgSendUtils.sendStringMsg(MsgType.DIALOGUE_CUSTOM_QUESTION, question);
                                                        dialogLoading.dismiss();
                                                        stroeVccLocal(finalAnswer, voiceType);
                                                }
                                        });
                                }
                                else {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        Logger.e("save", "onSuccess2222 file :" + "strMsgBean.getResult().toString()" + strMsgBean.getMsg().toString());
                                                        dialogLoading.dismiss();
                                                        failDialog.show();
                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                dialogLoading.dismiss();
                                                ToastTools.newInstance(RecordActivity.this).showText(getString(R.string.network_request_failed));
                                        }
                                });
                        }
                });

                // 默认传输文本是成功的，无需等待返回直接保存
//                stroeVccLocal(answer, voiceType);
        }

        /**
         * 保存对话信息到本地
         *
         * @param answer
         * @param type
         */
        private void stroeVccLocal(String answer, int type) {

                CustomDialogue customDialogue = new CustomDialogue(question, answer,
                                                                   type);
                ((MainApplication) getApplication()).mCustomDialogues
                        .add(customDialogue);
                MyDbUtils.add(this, question, answer, type);
                showDialog();
        }

        private  void  showDialog() {
                successDialog.show();// 成功提示
                new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                                successDialog.dismiss();
                                finish();
                        }
                }, 1 * 1000);
        }

        /**
         * 存储待机语信息
         */
        private void storeStandbyVoice() {

                HashMap<String, Object> hasMap = new HashMap<>();

                if (ConstUtils.VOICE_TYPE_FILE == voiceType) { // 文件
                        hasMap.put("voicetype", ConstUtils.VOICE_TYPE_TTS);
                        hasMap.put("fileName", musicPath);
                }
                else if (ConstUtils.VOICE_TYPE_TTS == voiceType) {// TTS
                        hasMap.put("voicetype", ConstUtils.VOICE_TYPE_FILE);
                        hasMap.put("tts", voiceStr);


                }
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getResources().getString(R.string.request));
                dialogLoading.show();

                hasMap.put("postype", "standby");
                hasMap.put("upload", "提交");

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "voicecustom", hasMap, new OkHttpUtils.OkHttpCallBack() {

                        @Override
                        public void onSuccess(String result) {

                                JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.dismiss();
                                                        storeStandbyVoiceLocal();
                                                }
                                        });
                                }
                                else {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.dismiss();
                                                        failDialog.show();
                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                dialogLoading.dismiss();
                                                ToastTools.newInstance(RecordActivity.this).showText(getString(R.string.network_request_failed));
                                        }
                                });
                        }
                });

        }

        /**
         * 保存待机语到本地
         */
        private void storeStandbyVoiceLocal() {
                SharedPreferences share = getSharedPreferences(ConstUtils.CUSTOM_VOICE,
                        MODE_PRIVATE);
                Editor edit = share.edit(); // 编辑文件
                if (!TextUtils.isEmpty(musicPath)) { // 文件
                        StandbyVoice standbyVoice = new StandbyVoice(
                                ConstUtils.VOICE_TYPE_FILE,
                                musicPath);
                        ((MainApplication) getApplication()).mStandbyVoices
                                .add(standbyVoice);
                }
                else if (!TextUtils.isEmpty(voiceStr)) {// TTS
                        StandbyVoice standbyVoice = new StandbyVoice(
                                ConstUtils.VOICE_TYPE_TTS,
                                voiceStr);
                        ((MainApplication) getApplication()).mStandbyVoices
                                .add(standbyVoice);
                }
                String contents = JSON
                        .toJSONString(((MainApplication) getApplication()).mStandbyVoices);
                edit.putString(ConstUtils.STANDBY_VOICE_CONTENT, contents);
                edit.apply();
                showDialog();
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode,
                                        Intent data) {
                if (resultCode == Activity.RESULT_CANCELED){
                        voiceType=0;
                        return;
                }
                if (requestCode == ConstUtils.CODE_SELECT_AUDIO && resultCode == RESULT_OK ) {

                        boolean isExist = data.getBooleanExtra("isChecked", false);
                        musicPath = data.getStringExtra("path");  //path为全路径
                        Logger.e(TAG, "选中的语音路径 ：" + musicPath);
                }
                switch (requestCode) {
//                        case ConstUtils.CODE_SELECT_AUDIO:
//                                musicPath = data.getStringExtra("filePaths");
//                                break;

                        case ConstUtils.CODE_SELECT_TTS:
                                voiceStr = data.getStringExtra("voiceStr");
                                break;
                }

        }


        public static void setDialogWindowAttr(Dialog dlg, Context ctx) {
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = (int) ctx.getResources().getDimension(R.dimen.x848);//宽高可设置具体大小
                lp.height = (int) ctx.getResources().getDimension(R.dimen.x499);
                dlg.getWindow().setAttributes(lp);
        }
}
